package org.javaboy.vhr.controller.salary;

import org.javaboy.vhr.model.RespBean;
import org.javaboy.vhr.model.Salary;
import org.javaboy.vhr.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.ByteArrayOutputStream;
import java.util.List;

@RestController
@RequestMapping("/salary/sob")
public class SalaryController {
    @Autowired
    SalaryService salaryService;

    /**
     * 查询所有工资方案
     * @return 工资方案列表
     */
    @GetMapping("/query")
    public List<Salary> getAllSalaries() {
        return salaryService.getAllSalaries();
    }

    /**
     * 添加工资方案
     * @param salary 工资方案对象
     * @return 响应结果
     */
    @PostMapping("/")
    public RespBean addSalary(@RequestBody Salary salary) {
        if (salaryService.addSalary(salary) == 1) {
            return RespBean.ok("添加成功!");
        }
        return RespBean.error("添加失败!");
    }

    /**
     * 根据ID删除工资方案
     * @param id 工资方案ID
     * @return 响应结果
     */
    @DeleteMapping("/{id}")
    public RespBean deleteSalaryById(@PathVariable Integer id) {
        if (salaryService.deleteSalaryById(id) == 1) {
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    /**
     * 更新工资方案
     * @param salary 工资方案对象
     * @return 响应结果
     */
    @PutMapping("/")
    public RespBean updateSalaryById(@RequestBody Salary salary) {
        if (salaryService.updateSalaryById(salary) == 1) {
            return RespBean.ok("更新成功!");
        }
        return RespBean.error("更新失败!");
    }

    /**
     * 导出所有工资方案为 Excel 文件
     * @return Excel 文件的二进制数据，供前端下载
     */
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportSalaryToExcel() {
        List<Salary> salaries = salaryService.getAllSalaries();
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("工资信息");
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("ID");
            headerRow.createCell(1).setCellValue("基本工资");
            headerRow.createCell(2).setCellValue("奖金");
            headerRow.createCell(3).setCellValue("午餐补贴");
            headerRow.createCell(4).setCellValue("交通补贴");
            headerRow.createCell(5).setCellValue("总工资");
            headerRow.createCell(6).setCellValue("养老金基数");
            headerRow.createCell(7).setCellValue("养老金比例");
            headerRow.createCell(8).setCellValue("医疗保险基数");
            headerRow.createCell(9).setCellValue("医疗保险比例");
            headerRow.createCell(10).setCellValue("公积金基数");
            headerRow.createCell(11).setCellValue("公积金比例");
            headerRow.createCell(12).setCellValue("名称");

            int rowNum = 1;
            for (Salary salary : salaries) {
                if (salary == null) continue;  // 添加空值检查，防止空指针异常
                Row row = sheet.createRow(rowNum++);
                row.createCell(0).setCellValue(salary.getId() != null ? salary.getId() : 0);
                row.createCell(1).setCellValue(salary.getBasicSalary() != null ? salary.getBasicSalary() : 0);
                row.createCell(2).setCellValue(salary.getBonus() != null ? salary.getBonus() : 0);
                row.createCell(3).setCellValue(salary.getLunchSalary() != null ? salary.getLunchSalary() : 0);
                row.createCell(4).setCellValue(salary.getTrafficSalary() != null ? salary.getTrafficSalary() : 0);
                row.createCell(5).setCellValue(salary.getAllSalary() != null ? salary.getAllSalary() : 0);
                row.createCell(6).setCellValue(salary.getPensionBase() != null ? salary.getPensionBase() : 0);
                row.createCell(7).setCellValue(salary.getPensionPer() != null ? salary.getPensionPer() : 0);
                row.createCell(8).setCellValue(salary.getMedicalBase() != null ? salary.getMedicalBase() : 0);
                row.createCell(9).setCellValue(salary.getMedicalPer() != null ? salary.getMedicalPer() : 0);
                row.createCell(10).setCellValue(salary.getAccumulationFundBase() != null ? salary.getAccumulationFundBase() : 0);
                row.createCell(11).setCellValue(salary.getAccumulationFundPer() != null ? salary.getAccumulationFundPer() : 0);
                row.createCell(12).setCellValue(salary.getName() != null ? salary.getName() : "");
            }

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            workbook.write(outputStream);
            byte[] excelData = outputStream.toByteArray();

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", "salary_info.xlsx");
            return new ResponseEntity<>(excelData, headers, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}